Visual C#

<-- Prev --------- Contents --------- Next -->

In order develop new robotics applications with NRS, we will be making programs on the server in Visual C# ("C-Sharp"). Visual C# is an Integrated Development Environment (IDE) - essentially, a program designed to help you make your own programs. IDE's help you manage the resources on a computer (code files, libraries, etc) while you program, highlight keywords in your code to make it much easier to read, and provide extremely useful debugging tools. Visual C# is Microsoft's IDE for programming in the C# programming language.

It's important to make a distinction between Visual C# and C#. Visual C# is an IDE - a program editor and project manager. C# itself is a programming language. We could have selected a different IDE to replace Visual C# without having to re-write any of our code. In fact, we don't need to use an IDE at all - it just makes things easier. Alternatively, we could have chosen a different programming language - C, C++, Java, etc - in which case we would have to re-write our code.

To begin programming, let's take a look at some of the most important features of Visual C#.

Start up Visual C# now.

In Visual C#, our program files will be collected into a project.

Create a new project now by selecting File->New Project from the main menu:
file_new_project.JPG

This will open up the New Project window. Visual C# has several project templates to make it easier to get started. There will be two project types that we will be using in this curriculum- "Windows Form Application" and "Console Application". A Windows Form Application is a program that is designed to have a graphical user interface (GUI)- basically a window with buttons, text boxes, etc. for intuitive user interaction. Most advanced applications will likely fall into this category. A Console Application is a program that has only text input and text output. This input/output ("IO") is displayed in a command line terminal window which is opened when the program is executed. A Console Application's interface is simple (and may not look impressive), but it is an easy way to view output from programs that don't require fancy graphics and complicated input. For our first few lessons (and the WiiKart project), we will be making Console Applications. Our second project will be a Windows Form Application.

For now, select "Console Application" from the New Project window, and type "ExampleProject" in the Name field, as shown below.

Then hit OK.
exampleprogram.JPG


Now that you have created a Console Application, you should see several things. The left part of your screen should be a tabbed window. The current tab should be named Program.cs. This is the code for the main program we will be writing. A .cs file is a text file that contains C# code. The text that you see in the tabbed window should look something like this:

empty_program.JPG

Don't worry about what it all means right now, but that's the C# code for our program (which doesn't do anything at the moment). We'll come back to what it all means, but for now let's continue looking at the different parts of the Visual C# window. As we mentioned, the code window on the left is tabbed - so you can have more than one .cs file from your project open at a time (and this will be very useful). On the topic of .cs files, take a look at the right side of the Visual C# window. You should see another window labeled "Solution Explorer", shown below:
solution_explorer.JPG

The solution explorer shows all of the files associated with our project. Notice that Program.cs is among them; that's our code file - and other .cs files that we create along the way will be added to this list. The Properties section can mostly be ignored for now. The References section, however, will come into play in our projects. It contains a list of the resources that are included in our program, and we will need to add several libraries to it later. Libraries are essentially collections of pre-written code (that someone else may have written) that can be used by other programs. For now, just know that we can use the Solution Explorer window to keep track of our files. Double-clicking a file in the Solution Explorer (such as the Program.cs file) will cause it to be opened in the left-hand window.

Let's run our empty program. To run a program that you have written, it must first be compiled. Remember from our discussion of the robot_program.hex file that when a program is compiled, it is converted from human-readable text to machine-readable instructions? Well we need to do that same thing here. Our program is currently saved as text in the Program.cs file. In order for our PC to be able to execute the program, we need to compile it. The green arrow at the top of the screen (shown below) will compile AND run our code. Alternatively, you can just hit the F5 key, or, choose Debug->Start Debugging from the main menu- they all do the same thing.

Go ahead and hit the green arrow, shown below.

execute_button.JPG

A few things should have happened. If you were watching closely, you would have seen a black window appear and then disappear very quickly - that was our console window. It opened when the program started, and then closed again when it ended... which was almost immediately. Second, a new subwindow opened in the bottom of the Visual C# window. This was the Debug Window. The Debug Window informs you of errors during program compiling or execution, and does it's best to tell you where the error is in your code - which may or may not be correct. If your program does generate errors, double click the error message, and Visual C# will take you to the suspected line of code in the Code Window.

That should be enough about Visual C# to get you started for now- Save and quit, and we can start writing real programs!

<-- Prev --------- Contents --------- Next -->